package org.dizem.groupon.dao.impl;

import org.dizem.groupon.dao.ItemDAO;
import org.dizem.groupon.model.domain.ItemEntity;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Date;
import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-26 下午8:09
 */
public class ItemDAOImpl extends DAOImplSupport<ItemEntity> implements ItemDAO {
	public ItemEntity getById(int id) {
		return (ItemEntity) getHibernateTemplate().get(ItemEntity.class, id);
	}

	public List<ItemEntity> loadAll() {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemEntity.class);
		dc.addOrder(Order.desc("beginTime"));
		return getHibernateTemplate().findByCriteria(dc);
	}

	public List<ItemEntity> load(int firstResult, int maxResult) {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemEntity.class);
		dc.addOrder(Order.desc("beginTime"));
		return getHibernateTemplate().findByCriteria(dc, firstResult, maxResult);
	}

	public List<ItemEntity> loadPastList(int firstResult, int maxResult) {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemEntity.class);
		dc.add(Restrictions.lt("endTime", new Date()));
		dc.addOrder(Order.desc("beginTime"));
		return getHibernateTemplate().findByCriteria(dc, firstResult, maxResult);
	}

	public List<ItemEntity> loadCurrentList(int firstResult, int maxResult) {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemEntity.class);
		dc.addOrder(Order.desc("beginTime"));
		dc.add(Restrictions.ge("endTime", new Date()));
		return getHibernateTemplate().findByCriteria(dc, firstResult, maxResult);
	}

	public int getPastCount() {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemEntity.class);
		dc.add(Restrictions.lt("endTime", new Date()));
		return getHibernateTemplate().findByCriteria(dc).size();
	}

	public int getCurrentCount() {
		DetachedCriteria dc = DetachedCriteria.forClass(ItemEntity.class);
		dc.add(Restrictions.ge("endTime", new Date()));
		return getHibernateTemplate().findByCriteria(dc).size();
	}
}
